package org.zjt.spark.etl

import com.redis.RedisClient
import org.json4s
import org.json4s.{DefaultFormats, NoTypeHints}
import org.json4s.JsonAST.JObject
import org.json4s.jackson.{JsonMethods, Serialization}
import org.json4s.jackson.Serialization.{read, write}

/**
  * DESC    
  *
  * @author
  * @create 2017-07-04 下午2:24
  **/
object ETLDomain extends App {
  val str = "{\"name\":\"zhangsan\",\"age\":12,\"studentNo\":212321}"
  implicit val formats = DefaultFormats
  val var2: Student = JsonMethods.parse(str).extract[Student]
  println(var2.age)
  val var3: Student = JsonMethods.parse(str).extract[Student]
  println(var3.age)


  implicit val format = Serialization.formats(NoTypeHints)


  val tConfContext = "{\"desc\":\"测试分表 \",\"lower\":-113,\"upper\":-110,\"period\":\"D\",\"dateFormat\":\"yyyy-MM-dd\",\"emails\":\"biao.tang@yeeyk.com\",\"isTimerRunner\":0,\"confExpression\":null,\"isSparkTask\":1,\"id\":\"FourthTxpSplit_2017-03-13\",\"active\":true}"
  val var4: TaskConf = read[TaskConf](tConfContext)
  println(var4.desc)
  println(write(var4))


  val eConfContext = "{\"sourceId\":\"Econfig\",\"taskId\":\"FourthTxpSplit_2017-03-13\",\"extractorType\":\"RdbExtractor\",\"colNameType\":{\"ORDERNO\":\"String\",\"MERCHANTNO\":\"String\",\"MERCHANTNAME\":\"String\",\"TYPE\":\"String\",\"AMOUNT\":\"String\",\"ORDERSTATUS\":\"String\",\"PAYNO\":\"String\",\"SUCCESSAMOUNT\":\"String\",\"COMPLETE_DATE\":\"String\",\"CHANNEL\":\"String\",\"RATE\":\"String\",\"CHANNELRATE\":\"String\"},\"active\":true,\"typeCleanNeeded\":true}"
  val eConf: EConf = read[EConf](eConfContext)
  println(eConf.sourceId)
  println(write(eConf))


  val t1ConfContext = "{\"id\":\"Tconfig\",\"taskId\":\"FourthTxpSplit_2017-03-13\",\"transformType\":\"primarytable\",\"ruleDesc\":null,\"tableName\":\"Econfig\",\"active\":true}"
  val tConf: TConf = read[TConf](t1ConfContext)
  println(tConf.id)
  println(write(tConf))


  val lConfContext = "{\"id\":\"Lconfig\",\"taskId\":\"FourthTxpSplit_2017-03-13\",\"loaderClassName\":\"RdbBatchLoader\",\"colNameType\":{\"ORDERNO\":\"String\",\"MERCHANTNAME\":\"String\",\"TYPE\":\"String\",\"MERCHANTNO\":\"String\",\"AMOUNT\":\"String\",\"ORDERSTATUS\":\"String\",\"PAYNO\":\"String\",\"SUCCESSAMOUNT\":\"String\",\"COMPLETE_DATE\":\"String\",\"CHANNEL\":\"String\",\"RATE\":\"String\",\"CHANNELRATE\":\"String,,aescryptEncrypt\"},\"dbname\":\"YYK_BIGDATA_DATA\",\"dbfrom\":\"FILE\",\"driverClassName\":null,\"url\":null,\"username\":null,\"password\":null,\"tableName\":\"T_TXP_ORDER_PAY\",\"updatesqlList\":null,\"etlTaskIdNeeded\":true,\"active\":true,\"colEncrypt\":false}"
  val lConf: LConf = read[LConf](lConfContext)
  println(lConf.id)
  println(write(lConf))


  val sConfContext = "{\"taskId\":\"FourthTxpSplit_2017-03-13\",\"id\":\"Econfig\",\"sourceType\":\"RdbExtractor\",\"dbname\":\"Fourth_Txp_Split\",\"dbfrom\":\"DB\",\"sql\":\"SELECT\\n\\tO.order_no as orderNo,\\n\\tO.merchant_no AS merchantNo,\\n\\tO.merchant_name as merchantName,\\n\\tO.pay_type\\tas TYPE,\\n\\tO.request_amount as amount,\\n\\tO.order_status as orderStatus,\\n\\tP.pay_no as payNo,\\n\\tP.order_no as orderNo ,\\n\\tP.amount_success  as successAmount,\\n\\tP.channel_flag AS channel,\\n\\tP.merchant_rate as rate,\\n\\tP.channel_rate as channelRate,\\n\\tP.complete_date as complete_date\\nFROM\\n\\t t_txp_order O\\nLEFT JOIN \\n        t_txp_pay P ON O.order_no = P.order_no\\nWHERE\\n\\tO.create_date >= '2017-03-13' and O.create_date < '2017-03-14' and P.create_date >= '2017-03-13'  and P.create_date < '2017-03-14'\",\"driverClassName\":\"com.mysql.jdbc.Driver\",\"url\":\"jdbc:mysql://172.19.110.233:3306/FOURTH_TXP?useUnicode=true&characterEncoding=UTF-8\",\"username\":\"yeex\",\"password\":\"VAxJVn3D0c54ccV3PG3xng==\",\"encrypted\":true,\"whole\":true,\"active\":true}"
  val sConf: RdbSourceConf = read[RdbSourceConf](sConfContext)
  println(sConf.id)
  println(write(sConf))


  val  etlConfContext ="{\"taskConf\":{\"desc\":\"测试分表 \",\"lower\":-113,\"upper\":-110,\"period\":\"D\",\"dateFormat\":\"yyyy-MM-dd\",\"emails\":\"biao.tang@yeeyk.com\",\"isTimerRunner\":0,\"confExpression\":null,\"isSparkTask\":1,\"id\":\"FourthTxpSplit_2017-03-13\",\"active\":true},\"eConfs\":[{\"sourceId\":\"Econfig\",\"taskId\":\"FourthTxpSplit_2017-03-13\",\"extractorType\":\"RdbExtractor\",\"colNameType\":{\"ORDERNO\":\"String\",\"MERCHANTNO\":\"String\",\"MERCHANTNAME\":\"String\",\"TYPE\":\"String\",\"AMOUNT\":\"String\",\"ORDERSTATUS\":\"String\",\"PAYNO\":\"String\",\"SUCCESSAMOUNT\":\"String\",\"COMPLETE_DATE\":\"String\",\"CHANNEL\":\"String\",\"RATE\":\"String\",\"CHANNELRATE\":\"String\"},\"active\":true,\"typeCleanNeeded\":true}],\"tConfs\":[{\"id\":\"Tconfig\",\"taskId\":\"FourthTxpSplit_2017-03-13\",\"transformType\":\"primarytable\",\"ruleDesc\":null,\"tableName\":\"Econfig\",\"active\":true}],\"lConfs\":[{\"id\":\"Lconfig\",\"taskId\":\"FourthTxpSplit_2017-03-13\",\"loaderClassName\":\"RdbBatchLoader\",\"colNameType\":{\"ORDERNO\":\"String\",\"MERCHANTNAME\":\"String\",\"TYPE\":\"String\",\"MERCHANTNO\":\"String\",\"AMOUNT\":\"String\",\"ORDERSTATUS\":\"String\",\"PAYNO\":\"String\",\"SUCCESSAMOUNT\":\"String\",\"COMPLETE_DATE\":\"String\",\"CHANNEL\":\"String\",\"RATE\":\"String\",\"CHANNELRATE\":\"String,,aescryptEncrypt\"},\"dbname\":\"YYK_BIGDATA_DATA\",\"dbfrom\":\"FILE\",\"driverClassName\":null,\"url\":null,\"username\":null,\"password\":null,\"tableName\":\"T_TXP_ORDER_PAY\",\"updatesqlList\":null,\"etlTaskIdNeeded\":true,\"active\":true,\"colEncrypt\":false}],\"sourceConfs\":[{\"taskId\":\"FourthTxpSplit_2017-03-13\",\"id\":\"Econfig\",\"sourceType\":\"RdbExtractor\",\"dbname\":\"Fourth_Txp_Split\",\"dbfrom\":\"DB\",\"sql\":\"SELECT\\n\\t\\n\\tO.merchant_no AS merchantNo,\\n\\tO.merchant_name as merchantName,\\n\\tO.pay_type\\tas TYPE,\\n\\tO.request_amount as amount,\\n\\tO.order_status as orderStatus,\\n\\tP.pay_no as payNo,\\n\\tP.order_no as orderNo ,\\n\\tP.amount_success  as successAmount,\\n\\tP.channel_flag AS channel,\\n\\tP.merchant_rate as rate,\\n\\tP.channel_rate as channelRate,\\n\\tP.complete_date as complete_date\\nFROM\\n\\t t_txp_order O\\nLEFT JOIN \\n        t_txp_pay P ON O.order_no = P.order_no\\nWHERE\\n\\tO.create_date >= '2017-03-13' and O.create_date < '2017-03-14' and P.create_date >= '2017-03-13'  and P.create_date < '2017-03-14'\",\"driverClassName\":\"com.mysql.jdbc.Driver\",\"url\":\"jdbc:mysql://172.19.110.233:3306/FOURTH_TXP?useUnicode=true&characterEncoding=UTF-8\",\"username\":\"yeex\",\"password\":\"VAxJVn3D0c54ccV3PG3xng==\",\"encrypted\":true,\"whole\":true,\"active\":true}]}"
  val etlConf: ETLConfiguration = read[ETLConfiguration](etlConfContext)
  println(sConf.id)
  println(write(etlConf))

  println(etlConfContext.replace("\"", "").length)
  println(write(etlConf).length)


  val redisClient = new RedisClient("172.19.110.223", 6379)
  redisClient.set("etl_conf", etlConfContext)

  val value = redisClient.get("etl_conf") match {
    case Some(a) => a
    case None => null
  }
  if (value != null) {
    val etlConf2: ETLConfiguration = read[ETLConfiguration](value)
    sys.error(write(etlConf2))
  }








}


/**
  * 类的定义：
  *
  * 1、case class: 定义所有的属性都是主构造器。var、val属性都是public，不能被继承。
  * 2、class : 主构造器中的var不能被访问，val可以被访问。
  * 4、class inner field： java类似用法，var可被修改。val不能被修改。
  *
  */


class ETLConfiguration(val taskConf: TaskConf, val eConfs: List[EConf], val tConfs: List[TConf], val lConfs: List[LConf], val sourceConfs: List[RdbSourceConf]) extends Serializable

class RdbSourceConf(override val id: String, override val active: Boolean, override val taskId: String, override val sourceType: String, override val dbname: String, override val dbfrom: String, val sql: String, val url: String, val username: String, val password: String, val whole: Boolean, val encrypted: Boolean) extends SourceConf(id, active, taskId, sourceType, dbname, dbfrom) with  Serializable

class SourceConf(val id: String, val active: Boolean, val taskId: String, val sourceType: String, val dbname: String, val dbfrom: String) extends Serializable

class LConf(val id: String, val active: Boolean, val taskId: String, val loaderClassName: String, val colEncrypt: Boolean, val dbname: String, val dbfrom: String, val colNameType: Map[String, String]) extends Serializable

class TConf(val id: String, val active: Boolean, val taskId: String, val transformType: String, val ruleDesc: String, val tableName: String) extends Serializable

class EConf(val sourceId: String, val taskId: String, val extractorType: String, val typeCleanNeeded: Boolean, val colNameType: Map[String, String]) extends Serializable

class TaskConf(val id: String, val desc: String, val active: Boolean = true, val lower: Int, val upper: Int, val period: String, val dateFormat: String, val emails: String, val isTimerRunner: Int, val confExpression: String, val isSparkTask: Int) extends Serializable

class Student(val name: String, val age: Int, val studentNo: Int)
